﻿using UnityEngine;
using System.Collections;

public class HelperClass : MonoBehaviour 
{
    public static Quaternion LookAt(Vector3 target, float speed, Vector3 position, Quaternion rotation, Vector3 fwd)
    {
        if (fwd == Vector3.zero)
            fwd = Vector3.forward;

        Vector3 tminusp = target - position;
        Vector3 ominusp = fwd;

        if (tminusp == Vector3.zero)
            return rotation;

        tminusp.Normalize();

        float theta = (float)System.Math.Acos(Vector3.Dot(tminusp, ominusp)) * Mathf.Rad2Deg;
        Vector3 cross = Vector3.Cross(ominusp, tminusp);

        if (cross == Vector3.zero)
            return rotation;

        cross.Normalize();

        Quaternion targetQ = Quaternion.AngleAxis(theta, cross);

        return Quaternion.Slerp(rotation, targetQ, speed);
    }
}
